package com.example.mydy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.IncomeRecord;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 收益详情适配器
 */
public class IncomeDetailAdapter extends RecyclerView.Adapter<IncomeDetailAdapter.ViewHolder> {

    private Context context;
    private List<IncomeRecord> incomeRecords;
    private SimpleDateFormat inputFormat;
    private SimpleDateFormat outputFormat;

    public IncomeDetailAdapter(Context context, List<IncomeRecord> incomeRecords) {
        this.context = context;
        this.incomeRecords = incomeRecords;
        this.inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        this.outputFormat = new SimpleDateFormat("MM-dd HH:mm", Locale.getDefault());
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_income_detail, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        IncomeRecord record = incomeRecords.get(position);
        
        // 设置收益金额
        holder.tvAmount.setText(String.format("+¥%.2f", record.getAmount()));
        
        // 设置收益类型，根据类型设置不同颜色
        holder.tvType.setText(record.getTypeText());
        switch (record.getType()) {
            case IncomeRecord.TYPE_TASK:
                holder.tvType.setTextColor(ContextCompat.getColor(context, R.color.colorSuccess));
                break;
            case IncomeRecord.TYPE_REWARD:
                holder.tvType.setTextColor(ContextCompat.getColor(context, R.color.colorWarning));
                break;
            case IncomeRecord.TYPE_DOWNLINE:
                holder.tvType.setTextColor(ContextCompat.getColor(context, R.color.colorInfo));
                break;
        }
        
        // 设置收益描述
        holder.tvDescription.setText(record.getDescription());
        
        // 格式化并设置时间
        try {
            Date date = inputFormat.parse(record.getCreateTime());
            if (date != null) {
                holder.tvTime.setText(outputFormat.format(date));
            } else {
                holder.tvTime.setText(record.getCreateTime());
            }
        } catch (ParseException e) {
            holder.tvTime.setText(record.getCreateTime());
        }
    }

    @Override
    public int getItemCount() {
        return incomeRecords.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvAmount;
        TextView tvType;
        TextView tvDescription;
        TextView tvTime;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvAmount = itemView.findViewById(R.id.tv_amount);
            tvType = itemView.findViewById(R.id.tv_type);
            tvDescription = itemView.findViewById(R.id.tv_description);
            tvTime = itemView.findViewById(R.id.tv_time);
        }
    }
} 